<template>
  <TeacherLayout>
    <div class="content">
      <el-card>
        <template #header>
          <div class="card-header">
            <span>教师工作台</span>
          </div>
        </template>
        <div class="dashboard-stats">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-card class="stat-card">
                <div class="stat-content">
                  <div class="stat-number">200</div>
                  <div class="stat-label">题目总数</div>
                </div>
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <div class="stat-content">
                  <div class="stat-number">5</div>
                  <div class="stat-label">当前考试</div>
                </div>
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <div class="stat-content">
                  <div class="stat-number">150</div>
                  <div class="stat-label">待批改</div>
                </div>
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <div class="stat-content">
                  <div class="stat-number">4</div>
                  <div class="stat-label">教授课程</div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </div>
        <div class="dashboard-info">
          <h3>欢迎使用教师工作台！</h3>
          <p>作为教师，您可以在此管理题库、创建考试、查询和批改学生成绩。</p>
          <p>请使用左侧菜单进行相应操作。</p>
          <el-divider />
          <h4>近期考试安排</h4>
          <el-timeline>
            <el-timeline-item timestamp="2024-01-20 09:00">高等数学期末考试</el-timeline-item>
            <el-timeline-item timestamp="2024-01-22 14:00">线性代数期中考试</el-timeline-item>
            <el-timeline-item timestamp="2024-01-25 10:00">概率论与数理统计测验</el-timeline-item>
          </el-timeline>
        </div>
      </el-card>
    </div>
  </TeacherLayout>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { get } from '../../utils/api'
import TeacherLayout from '../../components/teacher/TeacherLayout.vue'

const router = useRouter()
const userInfo = ref({ username: '', roleId: '', roleName: '' })

onMounted(() => {
  // 从本地存储获取用户信息
  const storedUserInfo = localStorage.getItem('userInfo')
  if (storedUserInfo) {
    userInfo.value = JSON.parse(storedUserInfo)
  } else {
    // 如果没有用户信息，跳转到登录页面
    router.push('/login')
  }
})

const handleLogout = async () => {
  try {
    // 调用后端登出接口，带上cookie
    await get('/user/logout')
    
    // 清除本地存储的用户信息
    localStorage.removeItem('user')
    
    ElMessage.success('退出登录成功')
    router.push('/login')
  } catch (error) {
    console.error('退出登录失败:', error)
    // 即使后端调用失败，也要清除本地状态并跳转
    localStorage.removeItem('user')
    ElMessage.success('退出登录成功')
    router.push('/login')
  }
}

const handleChangePassword = () => {
  router.push('/teacher/change-password')
}
</script>

<style scoped>
.dashboard-container {
  width: 100vw;
  height: 100vh;
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  overflow: hidden;
}

.main-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.el-header {
  background-color: #1890ff;
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-title-link {
  text-decoration: none;
}

.header-left h1 {
  margin: 0;
  font-size: 20px;
}

.sidebar {
  background-color: #fee;
  overflow-y: auto;
}

.el-menu {
  background-color: #fee;
  border-right: none;
}

.el-menu-item {
  color: #333;
}

.el-menu-item:hover {
  background-color: #fcc;
  color: #333;
}

.el-menu-item.is-active {
  background-color: #fcc;
  color: #333;
}

.el-sub-menu__title {
  color: #333;
}

.el-sub-menu__title:hover {
  background-color: #fcc;
  color: #333;
}

/* 确保二级菜单背景色和字体颜色正确 */
.el-sub-menu .el-menu {
  background-color: #fdd;
}

.el-sub-menu .el-menu-item {
  color: #333;
}

.content {
  padding: 20px;
  background-color: #f5f7fa;
  height: 100%;
  overflow-y: auto;
}

.dashboard-stats {
  margin-bottom: 20px;
}

.stat-card {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
}

.stat-number {
  font-size: 36px;
  font-weight: bold;
  margin-bottom: 10px;
}

.stat-label {
  font-size: 16px;
}

.dashboard-info {
  padding: 20px;
}

.dashboard-info h3,
.dashboard-info h4 {
  color: #333;
  margin-bottom: 10px;
}

.dashboard-info p {
  color: #666;
  line-height: 1.8;
}
</style>